﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing ;

using Google.GData.Calendar;
using Google.GData.Client;
using Google.GData.Extensions;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {

        static CalendarData f = new CalendarData(DateTime.Now, "goslowtest@gmail.com", "MyPassword");

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                buildTimeTable();
            }
            //accessData();

        }

        protected void accessData()
        {
            // Create a CalenderService and authenticate
            CalendarService myService = new CalendarService("exampleCo-exampleApp-1");
            myService.setUserCredentials("goslowtest@gmail.com", "MyPassword");

            CalendarQuery query = new CalendarQuery();
            query.Uri = new Uri("https://www.google.com/calendar/feeds/default/owncalendars/full");
            CalendarFeed resultFeed = (CalendarFeed)myService.Query(query);
            Console.WriteLine("Your calendars:\n");
            Google.GData.Calendar.EventEntry entry = new Google.GData.Calendar.EventEntry();

            entry.Title.Text = "Right from the application.";
            entry.Content.Content = "Yay.";
            myService.Insert(new Uri("https://www.google.com/calendar/feeds/default/private/full"), entry);

            foreach (CalendarEntry myEntry in resultFeed.Entries)
            {
                for (int i = 0; i < 50; i++) Response.Write("</br>");
            }
        }

        public void buildTimeTable()
        {
            AtomEntryCollection ee = f.getEntries();
            ExtensionCollection<When> thingsToAdd = new ExtensionCollection<When>();
            ArrayList eventTitles = new ArrayList();
            ArrayList eventContent = new ArrayList();

            foreach (AtomEntry e in ee)
            {
                Google.GData.Calendar.EventEntry eve = (Google.GData.Calendar.EventEntry)e;
                ExtensionCollection<When> times = eve.Times;
                foreach (When w in times)
                {
                    if (w.StartTime.ToShortDateString().Equals(myCal.SelectedDate.ToShortDateString()))
                    {
                        thingsToAdd.Add(w);
                        eventTitles.Add(eve.Title.Text);
                        eventContent.Add(eve.Content.Content);
                    }
                }
            }
            
            if (dayTable.Rows.Count == 0)
            {
                int rows = 12;
                int columns = 2;
                //Panel p = new Panel();
                //p.ID = "panel";
                //p.BackColor = System.Drawing.Color.Black;
                //Page.Controls.Add(p);
                //thr.Controls.Add(thc);
                //dayTable.Controls.Add(thr);
                dateTableLabel.Text = myCal.SelectedDate.ToShortDateString();
                for (int row = 0; row < rows; row++)
                {
                    TableRow nRow = new TableRow();
                    dayTable.Controls.Add(nRow);
                    for (int column = 0; column < columns; column++)
                    {
                        String textToAdd = "";
                        int h = row+(column*12);
                        foreach (When wh in thingsToAdd)
                        {
                            if ((Math.Abs(h - wh.StartTime.Hour)) < 1)
                            {
                                textToAdd += "Title: "+eventTitles[thingsToAdd.IndexOf(wh)]+"<br> Content: "+ eventContent[thingsToAdd.IndexOf(wh)]+" Starting at time "+wh.StartTime.ToShortTimeString();
                            }
                        }
                        TableCell nCell = new TableCell();
                        int time = 0;
                        if (row == 0)
                        {
                            time = rows - row;
                        }
                        else
                        {
                            time = row;
                        }
                        if (column == 0)
                        {
                            nCell.Text = time.ToString() + "am";
                        }
                        else
                        {
                            nCell.Text = time.ToString() + "pm";
                        }
                        nCell.Text += " " + textToAdd;
                        
                        nCell.ID = "tableCell" + row.ToString() + column.ToString();
                       

                        /*AjaxControlToolkit.HoverMenuExtender x = new AjaxControlToolkit.HoverMenuExtender();
                        Page.Controls.Add(x);
                        x.TargetControlID = nCell.ID;
                        x.PopupControlID = "panel3";
                        x.PopupPosition = AjaxControlToolkit.HoverMenuPopupPosition.Right;
                        x.PopDelay = 100;*/
                        nRow.Controls.Add(nCell);

                    }
                }
            }
            dayTable.BorderStyle = BorderStyle.Solid;
            dayTable.BorderWidth = Unit.Pixel(1);
        }
        protected void SelectionChanged(object sender, EventArgs e)
        {
            buildTimeTable();
            String dateString = myCal.SelectedDate.ToShortDateString();
            //DayLabel.Text = dateString;
            //ModalPopupExtender2.TargetControlID = "myCal";
            ModalPopupExtender1.Show();
            myCal.SelectedDate = DateTime.MaxValue;
            //Response.Write(DateTime.Now.CompareTo(myCal.SelectedDate));
            //e.Day.Date.CompareTo(DateTime.Now)
        }
        protected void ChangeLabel(object sender, EventArgs e)
        {
            
        }


        
        protected void SubmitData(object sender, EventArgs e)
        {
            //ModalPopupExtender2.Show();

            //right now start and end time must be in 24 hour form (0-23)

            String title = eventNameTextBox.Text;
            String startTime = eventStartTimeTextBox.Text;
            String endTime = eventEndTimeTextBox.Text;
            String eventContent = eventContentTextBox.Text;

            f = f.addEvent(f,title, eventContent, startTime, endTime);
        }
        protected void RenderDay(object sendet, DayRenderEventArgs e)
        {


                
            if (e.Day.Date.CompareTo(DateTime.Now.Date) == 0)
            {
                //e.Cell.ForeColor = e.Cell.BackColor;
                //e.Cell.Text = "YOU HAVE SOME IMPORTANT STUFF TO DO.";
                LiteralControl todayList = new LiteralControl("<br>It's today!");
                e.Cell.Controls.Add(todayList);
            }
            if (e.Day.Date.CompareTo(DateTime.Now.Date) < 0)
            {
                /*e.Cell.ForeColor = System.Drawing.Color.Gray;
                HtmlGenericControl dynDiv = new HtmlGenericControl("DIV");
                dynDiv.Style.Add(HtmlTextWriterStyle.BackgroundColor, "Red");
                dynDiv.Style.Add(HtmlTextWriterStyle.Height, "20px");
                dynDiv.Style.Add(HtmlTextWriterStyle.Width, "100px");
                e.Cell.Controls.Add(dynDiv);*/
            }
        }

        protected void Unnamed1_SelectionChanged(object sender, EventArgs e)
        {

        }
    }
}
